﻿using Abp.Application.Services.Dto;
using Microsoft.AspNetCore.Mvc;
using shenghua.Basics.Areas;
using shenghua.Basics.AreaSetting.Dto;
using shenghua.Basics.Setting.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.AreaSetting
{
    public class AreaAppService : shenghuaAppServiceBase, IAreaAppService
    {
        private readonly AreaManager _areaManager;

        public AreaAppService(
            AreaManager areaManager
            )
        {
            _areaManager = areaManager;
        }

        public async Task<AreaDto> Create(CreateAreaDto input)
        {
            Area area = new Area
            {
                ParentId = input.parentId,
                Name = input.Name,
                DisplayName = input.DisplayName,
                Enabled = input.Enabled
            };
            area = await _areaManager.Add(area);
            return ObjectMapper.Map<AreaDto>(area);
        }

        public async Task<AreaDto> Update(UpdateAreaDto input)
        {

            Area area = await _areaManager.Get(input.Id);
            area.DisplayName = input.DisplayName;
            await _areaManager.Update(area);
            return ObjectMapper.Map<AreaDto>(area);
        }

        [HttpPost]
        public async Task Delete(EntityDto input)
        {
            await _areaManager.Delete(input.Id);
        }

        public async Task SortUp(EntityDto input)
        {
            await _areaManager.SortUp(input.Id);
        }

        public async Task SortDown(EntityDto input)
        {
            await _areaManager.SortDown(input.Id);
        }

        public async Task SetEnabled(EntityDto input)
        {
            await _areaManager.Enabled(input.Id, true);
        }

        public async Task SetDisabled(EntityDto input)
        {
            await _areaManager.Enabled(input.Id, false);
        }

        public async Task<List<AreaDto>> GetAll(int? parentId)
        {
            var items = await _areaManager.GetAll(parentId);
            return ObjectMapper.Map<List<AreaDto>>(items);
        }

        public async Task<List<WordTreeDto<AreaDto>>> GetTree(int? parentId)
        {
            return ObjectMapper.Map<List<WordTreeDto<AreaDto>>>(await _areaManager.GetTree());
        }
    }
}
